package com.xuecheng.system.controller;

import cn.hutool.crypto.SecureUtil;
import com.xuecheng.commons.model.dto.LoginDto;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.UserVo;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.system.domain.User;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 用户信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    // 用户登录
    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginDto dto) {
        return userService.login(dto);
    }

    //验证手机验证码
    @PostMapping("/user/changePwd/verify")
    public ResponseResult verify(@RequestParam
                                              Map<String,String> param){
        // 1.接收请求参数
        String phone = param.get("phone");
        String code = param.get("verifyCode");
        return userService.verify(phone,code);
    }

    //根据id查询用户信息
    @GetMapping("/user")
    public ResponseResult phone(){
        Long userId = AuthInfoHolder.getUserId();
        User user = userService.getById(userId);
        UserVo userVo = BeanHelper.copyProperties(user, UserVo.class);
        return ResponseResult.okResult(userVo);
    }

    //修改密码
    @PostMapping("/user/changePwd/{verifyToken}")
    public ResponseResult changePwd(@PathVariable("verifyToken") String verifyToken,@RequestBody Map<String,String> param){
        //接收参数
        String password = param.get("password");//新密码

        String confirmPwd = param.get("confirmPwd");//确认密码
        String phone = param.get("phone");//手机号
        return userService.changePwd(password,confirmPwd,phone);


    }

}
